package sctu.edu.java.year2016.team07.project.admin;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;

/**
 * Created by Huang Suixin on 2016/6/30.
 */
public class Admin {
    Server server = new Server();

    //登陆后台的第一界面
    public Admin() {
        JFrame firstFrame = new JFrame();
        Container con = firstFrame.getContentPane();
        JPanel pan = new JPanel();
        pan.setOpaque(false);
        pan.setLayout(new GridLayout(2, 1));

//添加一个按钮，对问题库添加问题解决方案
        JPanel submitpan = new JPanel();
        submitpan.setOpaque(false);
        JButton submit = new JButton("增加解决方案");
        submit.setContentAreaFilled(false);     //设置按钮透明
        submit.setBorderPainted(false);     //取消按钮显示边界
        submit.setFont(new Font("dialog", 1, 20));
        submit.setForeground(Color.ORANGE);

        submit.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                JDialog submitDialog = new JDialog(firstFrame, "增加问题解决方案");
                Container con = submitDialog.getContentPane();
                JPanel allPan = new JPanel();


                //问题面板
                JPanel questionPan = new JPanel();
                JLabel questionLabel = new JLabel("问题：");
                questionLabel.setFont(new Font("dialog", 1, 20));
                JTextField questionText = new JTextField(20);
                questionPan.add(questionLabel);
                questionPan.add(questionText);

                //解决方案面板
                JPanel answerPan = new JPanel();
                JLabel answerLabel = new JLabel("解决方案：");
                answerLabel.setFont(new Font("dialog", 1, 20));
                JTextArea answerText = new JTextArea(20, 55);
                JScrollPane sp = new JScrollPane(answerText);
                answerPan.add(answerLabel);
                answerPan.add(sp);

                //提交按钮
                JPanel buttonPan = new JPanel();
                JButton button = new JButton("提交");
                button.setFont(new Font("dialog", 1, 20));
                button.setContentAreaFilled(false);     //设置按钮透明
                button.setBorderPainted(false);     //取消按钮显示边界
                buttonPan.add(button);
                button.addActionListener(new ActionListener() {
                    @Override
                    public void actionPerformed(ActionEvent e) {
                        server.addAnswer(questionText.getText(), answerText.getText());
                        submitDialog.dispose();
                    }
                });


                allPan.add(questionPan);
                allPan.add(answerPan);
                allPan.add(buttonPan);
                con.add(allPan);

                //设置窗口初始化
                submitDialog.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
                submitDialog.setBounds(firstFrame.getWidth() / 2, firstFrame.getHeight() / 2, 800, 600);
                submitDialog.setVisible(true);

            }
        });
        submitpan.add(submit);      //添加按钮

        //查看用户反馈界面的按钮
        JPanel actionpan = new JPanel();
        actionpan.setOpaque(false);
        JButton action = new JButton("查看用户反馈");
        action.setContentAreaFilled(false);     //设置按钮透明
        action.setBorderPainted(false);     //取消按钮显示边界
        action.setFont(new Font("dialog", 1, 20));
        action.setForeground(Color.ORANGE);
        action.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                actionSubmitButtonFrame();
            }
        });
        actionpan.add(action);


        //
        pan.add(submitpan);
        pan.add(actionpan);

        con.add(pan);
        //设置窗口背景
        try {
            ImageIcon background = new ImageIcon(ImageIO.read((new File("src\\main\\java\\sctu\\edu\\java\\year2016\\team07\\project\\image\\admin_background.jpg"))));
            JLabel label = new JLabel(background);
            label.setBounds(0, 0, background.getIconWidth(), background.getIconHeight());
            JPanel imagePanel = (JPanel) firstFrame.getContentPane();     //将内容窗格式转化成JPanel，否则不能使用方法setOpaque()来使内容窗格透明
            imagePanel.setOpaque(false);         //使用方法setOpaque()来是内容窗格透明
            imagePanel.setLayout(new FlowLayout());
            firstFrame.getLayeredPane().setLayout(null);

            firstFrame.getLayeredPane().add(label,new Integer(Integer.MIN_VALUE));

        } catch (IOException e) {
            e.printStackTrace();
        }
        //初始化
        firstFrame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
        firstFrame.setTitle("后台管理系统！");
        firstFrame.setBounds(300, 300, 500, 400);
        firstFrame.setResizable(false);     //设置窗口大小不可改变
        firstFrame.setAlwaysOnTop(true);        //设置总在最前面
        firstFrame.setVisible(true);
    }

    //显示用户反馈信息界面
    public void actionSubmitButtonFrame() {
        int fileNum = server.getRecordNum();     //反馈问题个数

        AdminFrame adminFrame = new AdminFrame();       //新建窗口
        Container con = adminFrame.getContentPane();
        JPanel panel = new JPanel();        //整个大的面板
        panel.setOpaque(false);
        panel.setLayout(new GridLayout(fileNum + 2, 0));        //设置为网格布局

        String panName[] = new String[fileNum];


        if (fileNum == 0) {
            JLabel jl = new JLabel("暂无用户反馈!");
            jl.setFont(new Font("dialog", 1, 20));
            jl.setForeground(Color.ORANGE);     //设置字体颜色
            JPanel jlpan = new JPanel();
            jlpan.setOpaque(false);
            jlpan.add(jl);
            panel.add(jlpan);
        }else{
            JLabel jl = new JLabel("有用户反馈信息：");
            jl.setFont(new Font("dialog", 1, 20));
            jl.setForeground(Color.ORANGE);     //设置字体颜色
            JPanel jlpan = new JPanel();
            jlpan.setOpaque(false);
            jlpan.add(jl);
            panel.add(jlpan);
        }

        //循环为panel添加每一个面板
        for (int i = 0; i < fileNum; i++) {
            panel.add(adminFrame.new Pan(server.getRecordName(i)));
        }


        //初始化
        con.add(panel);

        adminFrame.setVisible(true);

    }



//    public static void main(String[] args) {
//        new Admin();
//    }


}
